define(['jquery', 'bootstrap', 'backend', 'table', 'form','upload'], function ($, undefined, Backend, Table, Form,Upload) {

    var Controller = {
        index: function () {
            Form.api.bindevent($("form[role=form]")); // 加载富文本编辑器
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'group.name', title: __('Group')},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {
                            field: 'pay_type', title: __('会员组来源'),  formatter: function(value, row, index) {
                                // 判断pay_type的类型并返回相应的别名
                                switch (value) {
                                    case '1':
                                        return '微信支付';
                                    case '2':
                                        return '积分支付';
                                    case '3':
                                        return '申请赠送';
                                    case '4':
                                        return '管理员后台发放';
                                    default:
                                        return '无';
                                }
                            }
                        },
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'invitation_code', title: __('invitation_code')},
                        {field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        {field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true},
                        {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), formatter: function(value, row, index) {
                                // 判断pay_type的类型并返回相应的别名
                                let text, color;
                                switch (value) {
                                    case 'normal':
                                        text = '正常';
                                        color = '#18bc9c'; // 绿色
                                        break;
                                    case 'hidden':
                                        text = '隐藏';
                                        color = '#444c69'; // 灰色
                                        break;
                                    default:
                                        text = '待审核';
                                        color = '#f39c12'; // 黄色
                                        break;
                                }
                                // 返回带有样式的 HTML 元素
                                return `<span style="color: ${color};">${text}</span>`;

                            }, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons:[
                                {
                                    name: 'detail',
                                    text: __('Integral Flow'),
                                    title: __('Integral Flow'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: '/nYFpaxNKLR.php/score/index?ids={ids}',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                }
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        uploadServiceAgreement: function () {
            Controller.api.bindevent();
        }
    };

    //提交表单
    // Upload.api.plupload($("#faupload-avatar"), function(data, ret){
    //     Fast.api.ajax({
    //         url: '/nYFpaxNKLR.php/user/user/uploadServiceAgreement',
    //         data: {url: data.url}
    //     })
    // }, function(data, ret){
    //     Layer.alert(ret.msg);
    // });

    //富文本编辑器
    Form.api.bindevent($("form[role=form]"));

    //打开服务协议页面
    $(document).on('click', '#btn-import-file', function () {
            Layer.open({
                title: __('Service Agreement'),
                content: Fast.api.fixurl('/nYFpaxNKLR.php/user/user/uploadServiceAgreement'),
                type:2,
                area: ['80%','80%'],
                btn: ['保存'],
                yes: function (index, layero) {
                    //获取表达那元素
                    var iframeDoc = layero.find('iframe')[0].contentDocument;
                    var service_agreement = $(iframeDoc).find("#c-avatar").val();
                    var service_agreement_text = $(iframeDoc).find("#service_agreement_text").val();

                    $.ajax({
                        url: '/nYFpaxNKLR.php/user/user/uploadServiceAgreement',
                        method: 'POST',
                        data: {
                            'service_agreement':service_agreement,
                            'service_agreement_text':service_agreement_text,
                        }, // 序列化表单数据
                        success: function(response) {
                            // 处理服务器返回的响应
                            console.log(response);
                            Layer.close(index);//关闭窗口
                        },
                    });
                }
            });
    });

    return Controller;
});
